﻿using System.Collections.Specialized;
using System.Data;
using System.IO;
using System.Web;
using MuzoBlog.Core.BussinessLogic.Settings;

namespace MuzoBlog.Core.BussinessLogic.Languages
{
    public abstract class LanguageManager
    {
        private static Language _language; // TODO: Cache

        protected static StringDictionary LoadLanguage(string lang)
        {
            string fullPath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data/Language/"), lang + ".xml");
            DataSet ds = new DataSet();
            ds.ReadXml(fullPath);

            StringDictionary stringDictionary = new StringDictionary();
            for (int i = 0; i < ds.Tables["word"].Rows.Count; i++)
            {
                stringDictionary.Add(ds.Tables["word"].Rows[i]["key"].ToString(), ds.Tables["word"].Rows[i]["value"].ToString());
            }

            return stringDictionary;
        }

        static LanguageManager()
        {
            Reload();
        }

        public static void Reload()
        {
            _language = new Language();
            if (MuzoApp.Db == null)
            {
                _language.Site = LoadLanguage("en");
                _language.Admin = LoadLanguage("en");
            }
            else
            {
                _language.Site = LoadLanguage(SettingManager.GetStringValue("Language.Site"));
                _language.Admin = LoadLanguage(SettingManager.GetStringValue("Language.Admin"));
            }
        }

        public static string Get(string key)
        {
            return _language.Site[key];
        }

        public static string Admin(string key)
        {
            return _language.Admin[key];
        }
    }
}
